<?php

namespace Api\Controller;

use Api\Event\CacheEvent;

/**
 * @apiDefine ApiErrorExample
 *
 * @apiErrorExample 失败返回
 * HTTP/1.1 200 OK
 * {
 *  "code": "",
 *  "info": "",
 *  "data": "",
 * }
 *
 */

/**
 * @apiDefine ApiSuccessExample
 *
 * @apiSuccessExample 成功返回
 * HTTP/1.1 200 OK
 * {
 *  "data": "",
 *  "info": "",
 *  "code": 200,
 * }
 *
 */

/**
 * 应用App接口
 * @author Legend. <xcx_legender@qq.com>
 * @param
 * @return
 */

class AppController extends PublicController {

	/**
	 * 获取最新的版本信息
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	/**
	 * @api {get}  /api/App/version 获取APP/PC等应用最新版本信息
	 * @apiVersion 4.0.0
	 * @apiName version
	 * @apiGroup App
	 *
	 * @apiParam {String} type 应用类型标识 ['android','ios','pc']
	 * @apiSuccess {String} info  show message
	 * @apiSuccess {Number} code  status code.
	 * @apiSuccess {Object} data  extend datas.
	 * @apiSuccess {String} data.internal_version  内部版本号, 用于升级
	 * @apiSuccess {String} data.md5  MD5签名
	 * @apiSuccess {String} data.url  下载地址
	 * @apiSuccess {String} data.outer_version  外部版本号 用于显示
	 * @apiSuccess {Number} data.force_update  是否强制更新 [0,1]
	 *
	 * @apiUse ApiSuccessExample
	 *
	 * @apiUse ApiErrorExample
	 */
	public function version() {
		$event = A('Api/AppVersion', 'Event');
		$type = I('type', '', 'trim');
		if (!$type) {
			$this->param_error();
		}

		$ver = I('ver', '', 'intval');
		if (empty($ver)) {
			$data = $event->getLatest($type);
		} else {
			$data = $event->getVersionInfo($type, $ver);
		}
		if (!empty($data)) {
			if (!empty($data['update_info'])) {
				$data['update_info'] = json_decode($data['update_info'], true)?:'';
			} else {
				$data['update_info'] = '';
			}
			$activity_luckybox_time = C('ACTIVITY_LUCKYBOX_TIME');
			foreach ($activity_luckybox_time as &$value) {
				$time = explode('-', $value);
				$value = [
					'start_time' => strtotime($time[0]),
					'end_time'   => strtotime($time[1])
				];
			}
			$data['update_info']['luckybox_activity'] = $activity_luckybox_time;
		}
		$data['server_time'] = time();
		$this->success('', $data);
	}

	public function updateInfo()
	{
		$platform = I('platform', '', 'trim');
		switch ($platform) {
			case 'bigfoot':
				// 大脚客户端
				echo C('TOOL_UPDATE_INFO_BIGFOOT');
				break;
			case 'uploader':
				// 上传工具端
				echo C('TOOL_UPDATE_INFO_UPLOADER');
				break;
			default:
				// PC录制工具端
				echo C('TOOL_UPDATE_INFO');
				break;
		}
		exit;
	}

	// 修改配置参数
	public function setConfig($type = 'pc', $xml = '') {
		$types = ['pc' => 'PC_TOOL_XML'];
		if (!array_key_exists($type, $types) || !$xml) {
			$this->param_error();
		}
		$model = M('Config');
		$name = $types[$type];
		$data = $model->where(['name' => $name])->find();
		if (!$data) {
			$this->param_error();
		}
		$save['value'] = $xml;
		$model->where(['name' => $name])->save($save);
		$this->success();
	}

	// 获取配置参数
	public function getConfig($type = 'pc') {
		$types = ['pc' => 'PC_TOOL_XML'];
		if (!array_key_exists($type, $types)) {
			$this->param_error();
		}
		$model = M('Config');
		$name = $types[$type];
		$data = $model->where(['name' => $name])->find();
		$ret['xml'] = $data['value'];

		return $this->success($ret);

		////

		$types = ['pc' => 'PcXml'];
		if (!array_key_exists($type, $types)) {
			$this->param_error();
		}

		$service = service($types[$type]);

		$redis = CacheEvent::instance(1);

		$key = "IMBA.XML." . strtoupper($type);

		$xml = $redis->get($key);
		if (!$xml) {

			$res = $service->select();
			$xml = "<RecordClassifys>
			";
			$i = 1;
			foreach ($res as $key => $value) {
				$xml .= "<RecordClassifyItem>
		<Name>{$value['name']}</Name>
		<Icon>LOL.jpg</Icon>
		<ThumbnailIcon>lol_icon.jpg</ThumbnailIcon>
		<Type>Game</Type>
		<ID>{$i}</ID>
		<Details>
			<Game>
				<Window>{$value['window']}</Window>
				<WindowClass>{$value['class']}</WindowClass>
				<ExeName>{$value['exe']}</ExeName>
				<CaptureMouse>1</CaptureMouse>
			</Game>
		</Details>
	</RecordClassifyItem>
	";
				$i++;
			}
			$xml .= "<RecordClassifyItem>
		<Name>录制桌面</Name>
		<Icon>DeskTop.jpg</Icon>
		<ThumbnailIcon></ThumbnailIcon>
		<Type>Desktop</Type>
		<ID>{$i}</ID>
		<Details>
			<Desktop>
				<CaptureMouse>1</CaptureMouse>
				<CapturePointX>0</CapturePointX>
				<CapturePointY>0</CapturePointY>
				<CaptureWidth/>
				<CaptureHeight/>
			</Desktop>
		</Details>
	</RecordClassifyItem>
</RecordClassifys>
";
			$redis->set($key, 60, $xml);
		}
		$ret['xml'] = $xml;
		$this->success($ret);
	}

	public function new_version()
	{
		$type = I('type', '', 'trim');
		$ver = I('ver', '', 'intval');
		if (!$type || !in_array($type, array_keys(C('PHONE_OS')))) {
			$this->param_error();
		}
		$field = 'internal_version,detail,outer_version,create_time,log';
		$filter = [
			'internal_version' => ['$gt' => intval($ver)],
			'type' => $type
		];
		$lists = event('AppVersion')->lists($filter, $field);
		$this->success('', $lists);
	}
}